package com.swordliu.facebook.Utils;

import com.swordliu.facebook.pojo.entity.User;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * SingleLoginUtil
 * Created by SwordLiu on 2017-07-13.
 */
public class SingleLoginUtil {

    private static Map<String, HttpSession> sessionMap = new HashMap<>();

    public synchronized static void login(HttpSession session, String username) {
        if (sessionMap.containsKey(username)) {
            HttpSession ses = sessionMap.get(username);
            //如果是容一个session重复登录，可以不用kill掉，只有session不同时，才kill掉前一个登录的session
            if (!session.getId().equals(ses.getId())) {
                ses.invalidate();
            }
        }
        sessionMap.remove(username);
        sessionMap.put(username, session);
    }

}
